package com.csscis.ssm.web.controller;

import com.csscis.ssm.core.util.JsonUtils;
import com.csscis.ssm.web.model.ValveTable;
import com.csscis.ssm.web.service.ValveTableService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "/valve")
public class ValveController {

    @Autowired
    private ValveTableService valveTableService;


    @RequestMapping("/start")
    public String start(){
        return "valveList";
    }

    /**
     * 跳转到服务信息列表
     *
     * @return
     */
    @RequestMapping("/doValveList")
    public String doValveList() {
        return "valveList";
    }

    /**
     * 跳转到服务信息添加页面
     *
     * @return
     */
    @RequestMapping("/doValveAdd")
    public String doValveAdd(HttpServletRequest request) {

        request.setAttribute("type", "add");
        return "valveEdit";
    }

    /**
     * 查看服务列表
     * @param pageNo 当前页
     * @param pageSize 页面数据大小
     * @return 服务信息列表
     */
    @RequestMapping(value = "/List")
    @ResponseBody
    public String List(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            ValveTable valve) {
        return valveTableService.selectPageAndJson(pageNo, pageSize, valve);
    }

    /**
     *  添加服务信息
     * @param request
     * @param valveTable 服务信息对象
     * @return 信息列表
     */
    @RequestMapping(value = "/valveAdd")
    public String insert(HttpServletRequest request, ValveTable valveTable) {

        valveTableService.insert(valveTable, request);
        return "valveList";
    }

    /**
     * 修改服务信息
     * @param valveTable
     * @return
     */
    @RequestMapping(value = "/valveUpdate")
    public String update(ValveTable valveTable) {
        valveTableService.update(valveTable);
        return "valveList";
    }

    /**
     * 删除服务信息
     *
     * @param valveTableId 主键Id
     * @return
     */
    @RequestMapping(value = "/valveDel")
    public String delete(String valveTableId) {

        String[] ids = valveTableId.split(",");
        if (ids.length != 0) {
            for (String s : ids) {
                valveTableService.delete(Long.valueOf(s));
            }
        }
        return "valveList";
    }

    /**
     * 根据主键查询服务信息
     *
     * @param valveTableId
     * @param request
     * @return 编辑页面
     */
    @RequestMapping(value = "/valveById")
    public String queryById(Long valveTableId, HttpServletRequest request) {
        ValveTable valveTable = valveTableService.selectById(valveTableId);
        request.setAttribute("type", "update");
        request.setAttribute("valveTable", valveTable);
        return "valveEdit";
    }

    /**
     * 唯一性验证
     * @param model 对象
     * @param response
     */
    @RequestMapping(value = "/valveOnly")
    public void selectByModel(@Param("id") Long id, ValveTable model, HttpServletResponse response) {
        //selectAll 方法要写相应的条件
        List<ValveTable> list = valveTableService.selectAll(model);
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            ValveTable valve = list.get(i);
            if (valve.getValveTableId() != id) {
                lists.add(i, valve.getValveName());
            }
        }
        JsonUtils.checkResult(lists, response);
    }

}
